/**
方法一：记录所有请求，使用二分查找，找到第一个大于等于 target 的数
*/

export var RecentCounter = function () {
  this.pings = []
}

/**
 * @param {number} t
 * @return {number}
 */
RecentCounter.prototype.ping = function (t) {
  this.pings.push(t)

  const n = this.pings.length
  const target = t - 3000
  if (target <= 0) return n
  let [l, r] = [0, n]
  let targetIdx = 0
  while (l <= r) {
    const mid = (r + l) >> 1
    if (this.pings[mid] >= target) {
      targetIdx = mid
      r = mid - 1
    } else {
      l = mid + 1
    }
  }
  return n - targetIdx
}

/**
 * Your RecentCounter object will be instantiated and called as such:
 * var obj = new RecentCounter()
 * var param_1 = obj.ping(t)
 */
